Java. util. concurrent package source code reading 23 Fork/Join framework Fork tip, forkjoin
The previous article has been tracking the pushTask method of ForkJoinWorkerThread, and there is still no way to explain the Fork principle. Let's take a look at the run method of Fo
Java. util. concurrent package source code reading 26 Fork/Join framework Join, forkjoin
Next let's take a look at what happened to the join method called ForkJoinTask:
Public final V join () {// The doJoin method returns the stat
This article is from: http://ifeve.com/java-fork-join-framework/#more-35602
Translation sequence
Doug Lea The Great God introduced his paper on the Fork/join framework, which he wrote in Java 7.
Responsive programming (reacti
Introduction
"Less common in Java" This module has not been written for a long time, today write a Java fork/join distributed processing mode. Fork/join is introduced in JDK1.7, it can achieve simple map-reduce operation in some
When we need to do a lot of small tasks, experienced Java developers will use line pool to perform these small tasks efficiently. However, there is a task, for example, to sort an array of more than 10 million elements, which itself can be executed concurrently, but how to disassemble it into a small task needs to be split dynamically during the execution of a task. In this way, large tasks can be broken down into small tasks, small tasks can continue
() {
if (Mlength
Because the implementation of these methods above is defined in a subclass of Recursiveaction, you can create and run tasks directly in a forkjoinpool. The specific steps are as follows:
1. Create an object that represents the task you want to perform:
SRC represents the source picture pixel array
//DST that represents the generated picture of the pixel
forkblur fb = new Forkblur (src, 0, src.length, DST);
2. Create a Forkjoinpool instance of a running
Java-Fork/Join: java-forkjoin
Content: explanation on the Internet:
Step 1: Split the task. First, we need a fork class to split large tasks into subtasks. It is possible that the subtasks are still large, so we still need to keep splitting until the subtasks are small enou
;
Word = _w;
@Override protected Object Compute () {int result = 0;
if (End-start
Third, work theft
Forkjoinpool maintains a list of tasks for each thread, and when a task is complete, it can rearrange the tasks that hang on the full load thread to the idle thread. It is like two of people move bricks, first moved back to help the other person to carry the remaining bricks. This is called job theft, which solves the scheduling problem between tasks of different sizes. The job theft fe
Java multithreading ~~~ Synchronous and asynchronous Fork/Join frameworks
In the Fork/Join framework, there are two ways to submit a task: synchronous and asynchronous. The previously used invokeAll () method is synchronous, that is, any
After the task is submitted, this met
Last blog we introduced the thread synchronization through Cyclicbarrier, but the problem with this approach is that if a large task runs 2 threads to complete, if the thread 2 takes more than 1 twice times the thread, the thread 1 completes and waits for the thread 2 to complete, and the waiting process thread 1 cannot be reused. Now that we're ready to solve this problem, we want thread 1 to help thread 2 complete a portion of the task after completing its own task. Java7 as a
The Fork/join framework is to solve the problem by using multithreading to realize the divide-and-conquer method. Fork refers to the problem of constantly narrowing the scale, join refers to the results of the calculation of the child problem, to obtain a higher level of results.
The use of the
FinalInteger index = 5000;//the cardinality of the split task Private FinalInteger left; Private FinalInteger right; PublicFockJoin1 (integer left, integer right) { This. left =Left ; This. right =Right ; } @OverrideprotectedInteger Compute () {intsum = 0; if(Right-left //if the task is less than the cardinality, it is executed directly; like a recursive exit for(inti = left; I ) {sum+=i; } }Else{//tasks larger than cardinality, then split, similar to dichotomy, can also
process this folder and sends it toThe pool using the fork () method. This method sends the task to the pool that would execute itIf it has a free worker-thread or it can create a new one. The method returns immediately, soThe task can continue processing the content of the folder. For every file, a task compares itsExtension with the one it's looking for and, if they is equal, adds the name of the file to theList of results. Once the task has proc
sends it toThe pool using the fork () method. This method sends the task to the pool that would execute itIf it has a free worker-thread or it can create a new one. The method returns immediately, soThe task can continue processing the content of the folder. For every file, a task compares itsExtension with the one it's looking for and, if they is equal, adds the name of the file to theList of results. Once the task has processed all the content of
Fork-join FrameThe fork operation is to divide a large problem into several smaller problems. In this partitioning process is generally recursive. Until the calculation can be done directly. The size of the sub-problem needs to be properly selected. Too large a sub-problem is not conducive to improving performance in parallel, while too small sub-problems can hav
A simple example of Java-Fork/Join
Content: explanation on the Internet:
Step 1: Split the task. First, we need a fork class to split large tasks into subtasks. It is possible that the subtasks are still large, so we still need to keep splitting until the subtasks are small enough.
Step 2: Execute the task and merge t
that leverages multi-core architectures.
In this article, we will use the following three styles to compute the variance of a large dataset in different ways and compare it.
Style of command
Fork/join Framework
Streams API
Variance is a statistical concept that measures the degree of deviation of a group of numbers. The variance can be calculated by averaging the square sum of the difference between e
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.